package com.example.controller;

import com.example.pojo.*;
import com.example.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/step")
@CrossOrigin
public class OaStepController {
    @Autowired(required = false)
    IOaStepService oaStepService;

    @Autowired(required = false)
    IOaAttendService oaAttendService;

    @Autowired(required = false)
    IOaContractService oaContractService;

    @Autowired(required = false)
    IOaPermanageService oaPermanageService;

    @Autowired(required = false)
    IOaFeeService oaFeeService;

    @Autowired(required = false)
    IOaPostService oaPostService;

    @RequestMapping("/search")
    public java.util.List<OaStep> oaStepList(OaStep oaStep){
        java.util.List<OaStep> oaStepList = oaStepService.oaStepList(oaStep);
        if (oaStep.getStepKind()==null)
            return oaStepList;
        switch (oaStep.getStepKind()){
            case "费用申请":
                for (OaStep s:oaStepList){
                    OaFee oaFee = new OaFee();
                    oaFee.setStepId(s.getStepId());
                    oaFee = oaFeeService.oaFeeList(oaFee).get(0);
                    s.setFeeCost(oaFee.getFeeCost());
                    s.setFeeKind(oaFee.getFeeKind());
                    s.setFeeDetail(oaFee.getFeeDetail());
                }
                break;
            case "合同申请":
                for (OaStep s:oaStepList){
                    OaContract oaContract = new OaContract();
                    oaContract.setStepId(s.getStepId());
                    oaContract = oaContractService.oaContractList(oaContract).get(0);
                    s.setContractType(oaContract.getContractType());
                    s.setContractAparty(oaContract.getContractAparty());
                    s.setContractBparty(oaContract.getContractBparty());
                    s.setContractContent(oaContract.getContractContent());
                }
                break;
            case "考勤申请":
                for (OaStep s:oaStepList){
                    OaAttend oaAttend = new OaAttend();
                    oaAttend.setStepId(s.getStepId());
                    oaAttend = oaAttendService.oaAttendList(oaAttend).get(0);
                    s.setAttendType(oaAttend.getAttendType());
                    s.setAttendContent(oaAttend.getAttendContent());
                }
                break;
            case "人事变更申请":
                for (OaStep s:oaStepList){
                    OaPermanage oaPermanage = new OaPermanage();
                    oaPermanage.setStepId(s.getStepId());
                    oaPermanage = oaPermanageService.oaPermanageList(oaPermanage).get(0);
                    s.setPermanageDepartment(oaPermanage.getPermanageDepartment());
                    s.setPermanageTransfer(oaPermanage.getPermanageTransfer());
                    s.setPermanageSalary(oaPermanage.get__PermanageSalary());
                }
                break;
        }
        return oaStepList;
    }

    @RequestMapping("/insert")
    public int oaStepInsert(OaStep oaStep){ return  oaStepService.oaStepInsert(oaStep); }

    @RequestMapping("/update")
    public int oaStepUpdata(OaStep oaStep){ return  oaStepService.oaStepUpdata(oaStep); }

    @RequestMapping("/searchFuzzy")
    public java.util.List<OaStep> oaStepListFuzzy(String str){ return  oaStepService.oaStepListFuzzy(str); }

    @RequestMapping("/insertpermanage")
    public int oaStepInsertPermanage(OaStep oaStep){
        oaStepService.oaStepInsert(oaStep);
        OaPermanage oaPermanage = new OaPermanage();
        oaPermanage.setPermanageDepartment(oaStep.getPermanageDepartment());
        oaPermanage.setPermanageTransfer(oaStep.getPermanageTransfer());
        oaPermanage.set__PermanageSalary(oaStep.getPermanageSalary());
        java.util.List<OaStep> oaStepList = oaStepService.oaStepList(oaStep);
        oaStep = oaStepList.get(oaStepList.size()-1);
        oaPermanage.setStepId(oaStep.getStepId());
        return  oaPermanageService.oaPermanageInsert(oaPermanage);
    }

    @RequestMapping("/insertattend")
    public int oaStepInsertAttend(OaStep oaStep){
        oaStepService.oaStepInsert(oaStep);
        OaAttend oaAttend = new OaAttend();
        oaAttend.setAttendType(oaStep.getAttendType());
        oaAttend.setAttendContent(oaStep.getAttendContent());
        java.util.List<OaStep> oaStepList = oaStepService.oaStepList(oaStep);
        oaStep = oaStepList.get(oaStepList.size()-1);
        oaAttend.setStepId(oaStep.getStepId());
        return  oaAttendService.oaAttendInsert(oaAttend);
    }

    @RequestMapping("/insertcontract")
    public int oaStepInsertContract(OaStep oaStep){
        oaStepService.oaStepInsert(oaStep);
        OaContract oaContract = new OaContract();
        oaContract.setContractType(oaStep.getContractType());
        oaContract.setContractAparty(oaStep.getContractAparty());
        oaContract.setContractBparty(oaStep.getContractBparty());
        oaContract.setContractContent(oaStep.getContractContent());
        java.util.List<OaStep> oaStepList = oaStepService.oaStepList(oaStep);
        oaStep = oaStepList.get(oaStepList.size()-1);
        oaContract.setStepId(oaStep.getStepId());
        return  oaContractService.oaContractInsert(oaContract);
    }

    @RequestMapping("/insertfee")
    public int oaStepInsertFee(OaStep oaStep){
        oaStepService.oaStepInsert(oaStep);
        OaFee oaFee = new OaFee();
        oaFee.setFeeKind(oaStep.getFeeKind());
        oaFee.setFeeCost(oaStep.getFeeCost());
        oaFee.setFeeDetail(oaStep.getFeeDetail());
        java.util.List<OaStep> oaStepList = oaStepService.oaStepList(oaStep);
        oaStep = oaStepList.get(oaStepList.size()-1);
        oaFee.setStepId(oaStep.getStepId());
        return  oaFeeService.oaFeeInsert(oaFee);
    }
}
